package com.atguigu.gmall.item.service;

import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.item.feign.GmallPmsClient;
import com.atguigu.gmall.item.feign.GmallSmsClient;
import com.atguigu.gmall.item.feign.GmallWmsClient;
import com.atguigu.gmall.item.vo.ItemVO;
import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.vo.ItemGroupVO;
import com.atguigu.gmall.sms.vo.SaleVO;
import com.atguigu.gmall.wms.entity.WareSkuEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemService {

    @Autowired
    private GmallSmsClient smsClient;
    @Autowired
    private GmallPmsClient pmsClient;
    @Autowired
    private GmallWmsClient wmaClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    public ItemVO queryItemVO(Long skuId) {
        ItemVO itemVO = new ItemVO();
        //设置skuid
        itemVO.setSkuId(skuId);

        //根据id查询sku
        CompletableFuture<Object> skuCompletableFutrue = CompletableFuture.supplyAsync(() -> {
            Resp<SkuInfoEntity> skuResp = this.pmsClient.querySkuById(skuId);
            SkuInfoEntity skuInfoEntity = skuResp.getData();
            if (skuInfoEntity == null) {
                return itemVO;
            }
            itemVO.setSkuTitle(skuInfoEntity.getSkuTitle());
            itemVO.setSubTitle(skuInfoEntity.getSkuSubtitle());
            itemVO.setPrice(skuInfoEntity.getPrice());
            itemVO.setWeight(skuInfoEntity.getWeight());
            itemVO.setSpuId(skuInfoEntity.getSpuId());
            //获取spuid
            return skuInfoEntity;
        }, threadPoolExecutor);


        CompletableFuture<Void> spuCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            //根据sku中的spuid查询spu
            Resp<SpuInfoEntity> spuResp = this.pmsClient.querySpuById(((SkuInfoEntity) sku).getSpuId());
            SpuInfoEntity spuInfoEntity = spuResp.getData();
            if (spuInfoEntity != null) {
                itemVO.setSpuName(spuInfoEntity.getSpuName());
            }
        }, threadPoolExecutor);


        //根据skuid查询图片列表
        CompletableFuture<Void> imagesCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<SkuImagesEntity>> skuImagesResp = this.pmsClient.querySkuImagesBySkuId(skuId);
            List<SkuImagesEntity> skuImagesEntities = skuImagesResp.getData();
            itemVO.setPice(skuImagesEntities);
        }, threadPoolExecutor);


        //根据sku中的brandid和catgoryid查询品牌和分类
        CompletableFuture<Void> brandCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            Resp<BrandEntity> brandEntityResp = this.pmsClient.queryBrandById(((SkuInfoEntity) sku).getBrandId());
            BrandEntity brandEntity = brandEntityResp.getData();
            itemVO.setBrandEntity(brandEntity);
        }, threadPoolExecutor);


        CompletableFuture<Void> cateCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            Resp<CategoryEntity> categoryEntityResp = this.pmsClient.queryCategoryById(((SkuInfoEntity) sku).getCatalogId());
            CategoryEntity categoryEntity = categoryEntityResp.getData();
            itemVO.setCategoryEntity(categoryEntity);
        }, threadPoolExecutor);


        //根据skuid查询营销信息
        CompletableFuture<Void> saleCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<SaleVO>> saleResp = this.smsClient.querySalesBySkuId(skuId);
            List<SaleVO> saleVOList = saleResp.getData();
            itemVO.setSalse(saleVOList);
        }, threadPoolExecutor);


        CompletableFuture<Void> storeCompletableFuture = CompletableFuture.runAsync(() -> {
            //根据skuid 查询库存信息
            Resp<List<WareSkuEntity>> wareResp = this.wmaClient.queryWareSkuBySkuId(skuId);
            List<WareSkuEntity> wareSkuEntities = wareResp.getData();
            itemVO.setStore(wareSkuEntities.stream().anyMatch(wareSkuEntity -> wareSkuEntity.getStock() > 0));
        }, threadPoolExecutor);


        //根据spuid 查询所有skuid 再去查询所有的销售属性
        CompletableFuture<Void> saleAttrCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            Resp<List<SkuSaleAttrValueEntity>> saleAttrValueResp = this.pmsClient.querySkuSaleAttrValuesBySpuId(((SkuInfoEntity) sku).getSpuId());
            List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = saleAttrValueResp.getData();
            itemVO.setSaleAttrs(skuSaleAttrValueEntities);
        }, threadPoolExecutor);


        CompletableFuture<Void> descCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            //根据spuid查询商品描述(海报)
            Resp<SpuInfoDescEntity> spuInfoDescEntityResp = this.pmsClient.querySpuDescBySpuId(((SkuInfoEntity) sku).getSpuId());
            SpuInfoDescEntity descEntity = spuInfoDescEntityResp.getData();
            if (descEntity != null) {
                String decript = descEntity.getDecript();
                String[] split = StringUtils.split(decript, ",");
                itemVO.setImages(Arrays.asList(split));
            }
        }, threadPoolExecutor);


        CompletableFuture<Void> groupCompletableFuture = skuCompletableFutrue.thenAcceptAsync(sku -> {
            //根据spuid 和cateid查询组及组下的规格参数(带值)
            Resp<List<ItemGroupVO>> ItemGroupResp = this.pmsClient.queryItemGroupVOByCidAndSpuId(((SkuInfoEntity) sku).getCatalogId(), ((SkuInfoEntity) sku).getSpuId());
            List<ItemGroupVO> itemGroupVOS = ItemGroupResp.getData();
            itemVO.setGroups(itemGroupVOS);
        }, threadPoolExecutor);


        CompletableFuture.allOf(spuCompletableFuture, imagesCompletableFuture, brandCompletableFuture, cateCompletableFuture, saleCompletableFuture
                , storeCompletableFuture, saleAttrCompletableFuture, descCompletableFuture, groupCompletableFuture).join();


        return itemVO;
    }
}
